#include <iostream>
#include <fstream>
#include <set>
#include <iterator>
#include <map>
#include <list>
#include <ctime>
#include "product.h"
#include "warehouse.h"

using namespace std;
using namespace a4;

void printDate(std::tm day)
{
    mktime(&day);
    cout << day.tm_mon << "/" << day.tm_mday << "/" << day.tm_year  << endl;
    
}

int main()
{
    ifstream in("data1.txt");
    
    // Loop for reading the file.  Note that it is controlled
    //   from within the loop (see the 'break').
    
    
    set<string> *valid_UPCs = new set<string>();
    set<a4::warehouse*> *warehouse_set = new set<a4::warehouse*>();
    map<string,pair<string,int>> *UPC_lookup = new map<string,pair<string,int>>();
    
    while (true)
    {
        // Read a word (don't worry about punctuation)
        
        string word;
        in >> word;
       
        
        
               // If the read failed, we're probably at end of file
        //   (or else the disk went bad).  Exit the loop.
        if(word == "FoodItem")
        {
            string UPC;
            string name;
            int shelflife;
            getline(in, word,':');
            in >> UPC;
            getline(in, word,':');
            in >> shelflife;
            getline(in, word,':');
            getline(in, name,'\r');
            
            valid_UPCs->insert(UPC);
            pair<string,int> temp;
            temp.first = name;
            temp.second = shelflife;
            UPC_lookup->insert (pair<string,pair<string,int>>(UPC,temp));
            
            
            
            
        }
        if(word == "Warehouse")
        {
            string warehouse_name;
					getline(in , word, '-');
            getline(in,warehouse_name,'\r');
					while (warehouse_name[0] == ' ')
					{
						warehouse_name.erase(0, 1);
					}
				   
           warehouse *warehouse = new a4::warehouse(warehouse_name,UPC_lookup);
					 
					
						set<string>::iterator iter;
					for(iter = valid_UPCs->begin(); iter != valid_UPCs->end(); iter++)
					{
						string tempUPC = *iter;
						pair<string,int> temp_product_info = UPC_lookup->find(tempUPC)->second;
					  warehouse->addProduct(tempUPC,temp_product_info.second);
					}
					
					
           warehouse_set->insert(warehouse);
					 
           
        }
        if(word == "Start")
        {
            tm time;
            getline(in, word,':');
            
            int month,day,year;
            in >> month;
            
            getline(in,word,'/');
            in >> day;
            
            getline(in,word,'/');
            
            in >> year;
            
            time.tm_mon = month;
            time.tm_mday = day;
            time.tm_year = year;
            time.tm_sec = time.tm_min = time.tm_hour = 0;
            
            
            set<warehouse*>::iterator whs;
            for(whs = warehouse_set->begin(); whs != warehouse_set->end(); whs++)
            {
                warehouse* temp = *whs;
                temp->set_start_day(time);
                
            }
        
        }
        if(word == "Receive:")
        {
					
            string UPC;
            int number;
            string whouse;
            in >> UPC;
            in >> number;
            in >> whouse;
					
					while (whouse[0] == ' ')
					{
						whouse.erase(0, 1);
					}
					
					set<warehouse*>::iterator whs;
					for(whs = warehouse_set->begin(); whs != warehouse_set->end(); whs++)
					{
					
						warehouse* temp = *whs;
						if(temp->name == whouse)
						{
							temp->receive_item(UPC, number);
							break;
						}
						
					}
					
					          
        }
        if(word == "Request:")
        {
            string UPC;
            int number;
            string whouse;
            in >> UPC;
            in >> number;
            in >> whouse;
					
					set<warehouse*>::iterator whs;
					for(whs = warehouse_set->begin(); whs != warehouse_set->end(); whs++)
					{
						
						warehouse* temp = *whs;
						if(temp->name == whouse)
						{
							temp->satisfy_request(UPC, number);
							break;
						}
						
					}
            
            
        }
        if(word == "Next")
        {
					set<warehouse*>::iterator whs;
					for(whs = warehouse_set->begin(); whs != warehouse_set->end(); whs++)
					{
						warehouse* temp = *whs;
						temp->begin_new_day();
						
					}
					
            getline(in, word,'\r');
          
            
        }
			if(word == "End")
			{
				set<warehouse*>::iterator whs;
				for(whs = warehouse_set->begin(); whs != warehouse_set->end(); whs++)
				{
					warehouse* temp = *whs;
					temp->endOfList();
					
				}
				
				getline(in, word,'\r');
				
				
			}
        if (in.fail())
          
            break;
        
        
    }
	
	cout << "Report By Adam Bradford and Tim Grant" << endl;
	cout << endl;
	
	cout << "Unstocked Products:" << endl;
	
		
		
		
		set<string>::iterator iter;
		for(iter = valid_UPCs->begin(); iter != valid_UPCs->end(); iter++)
		{
			bool unstocked = true;
			string tempUPC = *iter;
			set<warehouse*>::iterator whs;
			for(whs = warehouse_set->begin(); whs != warehouse_set->end(); whs++)
			{
				warehouse* temp = *whs;
				if(temp->isStocked(tempUPC))
				{
					unstocked = false;
					break;
				}
			}
			if(unstocked)
			{
				pair<string,int> temp_pair = UPC_lookup->find(tempUPC)->second;
				
				string description = temp_pair.first;
				cout << tempUPC << " " <<  description << endl;
			}
			
		}
	cout << endl;
	
	
	//Fully Stocked Products output
	cout << "Fully Stocked Products:" << endl;
	for(iter = valid_UPCs->begin(); iter != valid_UPCs->end(); iter++)
	{
		bool stocked = true;
		string tempUPC = *iter;
		set<warehouse*>::iterator whs;
		for(whs = warehouse_set->begin(); whs != warehouse_set->end(); whs++)
		{
			warehouse* temp = *whs;
			if(!temp->isStocked(tempUPC))
			{
				stocked = false;
				break;
			}
		}
		if(stocked)
		{
			pair<string,int> temp_pair = UPC_lookup->find(tempUPC)->second;
			
			string description = temp_pair.first;
			cout << tempUPC << " " <<  description << endl;
		}
		
	}

	cout <<endl;
	
	//Busiest days
	cout << "Busiest Days:" << endl;
	
		set<warehouse*>::iterator whs;
		for(whs = warehouse_set->begin(); whs != warehouse_set->end(); whs++)
		{
			warehouse* temp = *whs;
			string name = temp->name;
			tm busiest_day = temp->get_busiest_day().first;
			int transactions = temp->get_busiest_day().second;
			
			cout << name << " " <<busiest_day.tm_mon << "/" << busiest_day.tm_mday << "/" << busiest_day.tm_year << " " << transactions << endl;
		}
		
		
	
}


